package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.MeBean;
import com.tybbank.Bean.YZMBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.ActivityUtil;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.CountDownButtonHelper;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/11/27 0027.
 */

public class SellGoldActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.et_http)
    EditText etHttp;
    @BindView(R.id.et_pay_pwdnum)
    EditText etPayPwdnum;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_veizhu)
    TextView tvVeizhu;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.bt_yanzhengma)
    Button btYanzhengma;
    @BindView(R.id.ll_gone_yzm)
    LinearLayout llGoneYzm;
    @BindView(R.id.et_phone)
    EditText etPhone;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sell_gold);
        ButterKnife.bind(this);
        toolbar(toolbar, getString(R.string.tibi), R.mipmap.icon_close, getString(R.string.jilu));
        ActivityUtil.beizhu(SellGoldActivity.this, 7, tvVeizhu);//  购买说明
        ini();
    }

    private void ini() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.user_detail)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG我的" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            MeBean enterBean = new Gson().fromJson(json, new TypeToken<MeBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                tvNum.setText(enterBean.getMoney().getAbc_coin());
                                etHttp.setText(enterBean.getUser_detail().getAbc_money_path());
                            } else if (status == 1004) {
                                Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();

                        }
                    }
                });
    }


    @OnClick({R.id.bt_yanzhengma, R.id.et_http, R.id.bt_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.et_http:
                break;
            case R.id.bt_yanzhengma:    //验证码
                if (TextUtils.isEmpty(etPhone.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.yourphone));
                    return;
                }else {
                String time = MD5Utils.getTime();
                SortedMap<String, String> parameters = new TreeMap<String, String>(
                        new Comparator<String>() {
                            public int compare(String obj1, String obj2) {
                                // 降序排序
                                return obj1.compareTo(obj2);
                            }
                        });
                parameters.put("TimeStamp", time);
                parameters.put("type", "3");
                    parameters.put("phone", etPhone.getText().toString().trim());
                parameters.put("uuid", MyApplication.uuid);
                String sign = MD5Utils.createSign(parameters);
                getokhttp(time, sign);
                }
                break;
            case R.id.bt_enter:
                if (TextUtils.isEmpty(etNum.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.hintnum));

                    return;
                } else if (TextUtils.isEmpty(etHttp.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.jiaoyisuoidhit));
                    return;
                } else if (TextUtils.isEmpty(etPhone.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.yourphone));
                    return;
                } else if (TextUtils.isEmpty(etYzm.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.sryzm));
                    return;
                } else if (TextUtils.isEmpty(etPayPwdnum.getText().toString().trim())) {
                    logUtilssss.toast(this, getString(R.string.paypwdhint));
                    return;
                } else {
                    init();
                }
                break;
        }
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("number", etNum.getText().toString().trim());
        parameters.put("money_path", etHttp.getText().toString().trim());
        parameters.put("verify_code", etYzm.getText().toString().trim());
        parameters.put("pay_pwd", MD5Utils.md5Password(etPayPwdnum.getText().toString().trim()));
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.propose)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("number", etNum.getText().toString().trim())
                .addParams("verify_code", etYzm.getText().toString().trim())
                .addParams("money_path", etHttp.getText().toString().trim())
                .addParams("pay_pwd", MD5Utils.md5Password(etPayPwdnum.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG可用提现" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                                setResult(RESULT_OK);
                                SellGoldActivity.this.finish();
                            } else if (status == 1004) {
                                Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }

                        } catch (Exception e) {

                        }
                    }
                });

    }

    //   短信接口
    private void getokhttp(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.SMS)
                .addParams("type", "3")
                .addParams("TimeStamp", time)
                  .addParams("phone", etPhone.getText().toString().trim())
                .addParams("Sign", sign)
                .addParams("uuid", MyApplication.uuid)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG短信接口" + response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                //    //短信请求的的代码
                                CountDownButtonHelper helper = new CountDownButtonHelper(btYanzhengma,
                                        getString(R.string.sent), 60, 1);
                                helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
                                    @Override
                                    public void finish() {
                                        btYanzhengma.setText(getString(R.string.hqyzm));
                                    }
                                });
                                helper.start();
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        } catch (Exception e) {
                        }
                    }
                });
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        SellGoldActivity.this.finish();
    }

    @Override
    public void RightNext() {
        super.RightNext();
        Intent intent = new Intent(this, BuySellListActivity.class);
        intent.putExtra("type", "2");
        startActivity(intent);
    }

}
